package com.xhtech.exercise.ui.ac

import androidx.activity.OnBackPressedCallback
import androidx.navigation.Navigation
import com.blankj.utilcode.util.ToastUtils
import com.xhtech.exercise.R
import com.xhtech.exercise.databinding.ActivityMainBinding
import me.hgj.jetpackmvvm.network.manager.NetState
import me.luochenyxh.base.base.BaseDataBindingActivity

class MainActivity : BaseDataBindingActivity<ActivityMainBinding>() {
    var exitTime = 0L
    override fun onCreated() {
        super.onCreated()

        //进入首页检查更新 腾讯Bugly
//        Beta.checkUpgrade(false, true)

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                val nav = Navigation.findNavController(this@MainActivity, R.id.host_fragment)
                if (nav.currentDestination != null && nav.currentDestination!!.id != R.id.mainfragment) {
                    //如果当前界面不是主页，那么直接调用返回即可
                    nav.navigateUp()
                } else {
                    //是主页
                    if (System.currentTimeMillis() - exitTime > 2000) {
                        ToastUtils.showShort("再按一次退出程序")
                        exitTime = System.currentTimeMillis()
                    } else {
                        finish()
                    }
                }
            }
        })
    }


    /**
     * 示例，在Activity/Fragment中如果想监听网络变化，可重写onNetworkStateChanged该方法
     */
    override fun onNetworkStateChanged(netState: NetState) {
        super.onNetworkStateChanged(netState)
        if (netState.isSuccess) {
            ToastUtils.showShort("我特么终于有网了啊!")
        } else {
            ToastUtils.showShort("我特么怎么断网了!")
        }
    }


}